let startBtn = document.querySelector(".startBtn");
let prizeCell = document.querySelectorAll(".drawBox div:not(.startBtn)");
let win = document.querySelector(".win");
let win1 = document.querySelector(".win1");

let index = 0;
let targetIndex = 84;
let timer;
let second = 200;
let isRun = true; //开关
let nowIndex = 0




// startBtn.addEventListener("click", function() {
//     timer = setInterval(function() {
//         (index = parseInt(Math.random() * 8)) && (index++);
//         if (index) {
//             prizeCell[(index - 1) % 8].classList.remove("active");
//         }

//         prizeCell[index % 8].classList.add("active");
//         if (index == targetIndex) {
//             clearInterval(timer);
//         }
//         index++;
//         console.log(index);
//     }, 200)
// })


/**
 * 开始抽奖(升级版)
 */
startBtn.addEventListener("click", function() {
    if (isRun) { //为true
        isRun = false;
        run();
    }


})

function run() {
    setTimeout(function() {

        if (index) {
            prizeCell[(index - 1) % 8].classList.remove("active");
        }

        prizeCell[index % 8].classList.add("active");

        if (nowIndex < targetIndex) {
            run();
        } else {
            isRun = true;
        }
        win1.innerHTML = prizeCell[index % 8].lastElementChild.innerHTML;
        win.innerHTML = prizeCell[index % 8].firstElementChild.outerHTML;
        nowIndex++;
        let index1 = [];
        let num = parseInt(Math.random() * 8);
        if (!index1.includes(num)) {
            index1.push(num);
        }
        index = index1;





        if (second > 30 && index < 20) {
            //加速
            second -= 5;
        } else if (index > 65) {
            //减速
            second += 15;
        }
    }, second)
}